function wdeChart(id) {
    this.dataMap = {
        "0":{
            title: '2017年行政分区废污水排放量',
            serieData: [{
                type: 'pie',
                size: '80%',
                name: '分配比例',
                data: [{y:3,
                    name:'城镇居民'},
                    {y:5,
                        name:'第二产业'},
                    {y:7,
                        name:'第三产业'}]
            }]
        },
        "1":{
            title:'2017年水资源分区废污水排放量',
            serieData: [{
                type: 'pie',
                size: '80%',
                name: '分配比例',
                data: [{y:4,
                    name:'城镇居民'},
                    {y:6,
                        name:'第二产业'},
                    {y:2,
                        name:'第三产业'}]
            }]
        }
    };
    this.chartid = null;
    this.chartType = "0";
}

wdeChart.prototype = {
    init: function (id) {
        var data = this.dataMap[this.chartType];
        this.chartid = id;
        $('#' + id).highcharts(this.getOption(data));
    },
    change: function () {
        var data = this.dataMap[this.chartType];
        $('#' + this.chartid).highcharts(this.getOption(data));
    },
    getOption: function (data) {
        return {
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: data.title
            },
            tooltip: {
                headerFormat: '{series.name}<br>',
                pointFormat: '{point.name}: <b>{point.percentage:.1f}%</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                        style: {
                            color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                        }
                    },
                    showInLegend: true
                }
            },
            series: data.serieData,
            credits: { enabled: false }
        };
    }
}

var _wdeChart = new wdeChart();

function wdeDataFactory () {
    this.tableData = null;
    this.activeBtn = 1;
    var me = this;
    // 获取蓄水动态评价表格数据
    this.loadData = function () {
        var def = $.Deferred();
        $.getJSON("/jsite-szy-dispatch/a/szy/dispatch/ddsWeDa/listAllToMap", function (data) {
            me.tableData = data;
            def.resolve();
        });
        return def.promise();
    }
}

wdeDataFactory.prototype = {
    // 从tableData中获取年份数据
    getYearsFromTableData: function () {
        var data = this.tableData;
        var result = [];
        for (var year in data) {
            result.push({
                'text': year,
                'value': year
            });
        }
        return result;
    },
    getChartData: function () {
        var data = this.tableData;
        var result = {},titleYear=2017,data0_townLifeDa=[],data0_second=[],data0_srvDa=[],data1_townLifeDa=[],data1_second=[],data1_srvDa=[];
        for (var year in data) {
            titleYear=parseInt(year);
            for(var temp=0,l=data[year].length;temp<l;temp++){
                var tempdata=data[year][temp];
                if(tempdata.regType==0){
                    data0_townLifeDa.push(parseFloat(tempdata.townLifeDa));
                    data0_second.push(parseFloat(tempdata.second));
                    data0_srvDa.push(parseFloat(tempdata.srvDa));
                }else if(tempdata.regType==1){
                    data1_townLifeDa.push(parseFloat(tempdata.townLifeDa));
                    data1_second.push(parseFloat(tempdata.second));
                    data1_srvDa.push(parseFloat(tempdata.srvDa));
                }
            }
        }
        result={
            "0":{
                title: titleYear+'年行政分区废污水排放量',
                serieData: [{
                    type: 'pie',
                    size: '80%',
                    name: '分配比例',
                    data: [{y:getAVG(data0_townLifeDa),
                        name:'城镇居民'},
                        {y:getAVG(data0_second),
                            name:'第二产业'},
                        {y:getAVG(data0_srvDa),
                            name:'第三产业'}]
                }]
            },
            "1":{
                title: titleYear+'年水资源分区废污水排放量',
                serieData: [{
                    type: 'pie',
                    size: '80%',
                    name: '分配比例',
                    data: [{y:getAVG(data1_townLifeDa),
                        name:'城镇居民'},
                        {y:getAVG(data1_second),
                            name:'第二产业'},
                        {y:getAVG(data1_srvDa),
                            name:'第三产业'}]
                }]
            }
        }
        return result;
    },
    setTableDataByType: function () {
        var year = Ext.getCmp("wdeCombo").getValue();
        var currentData = this.tableData[year];
        var type = this.activeBtn;
        var grepData = $.grep(currentData, function (obj) {
            return obj.regType == type;
        });
        Ext.data.StoreManager.lookup('wde_store1').setData(grepData);
    }
}


Ext.define('Admin.view.evaluation.wde.WdeController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.wdeController',
    views: ['Admin.view.evaluation.wde.Wde'],
    buildChart: function (dom) {
        var chartId = dom.el.id + "-body";
        _wdeChart.init(chartId);
    },
    initData: function (_this, opt) {
        var myMask = new Ext.LoadMask(Ext.ComponentQuery.query('wde')[0], { msg: "正在加载..." });
        myMask.show();
        var me = this;
        wdeData = new wdeDataFactory();
        $.when(wdeData.loadData())
            .done(function () {
                _wdeChart.dataMap=wdeData.getChartData();
                // 设置年份数据
                var yearData = wdeData.getYearsFromTableData();
                var firstSelect = yearData.length > 0 ? yearData[0].value : '';
                var year_store = Ext.create('Ext.data.Store', {
                    fields: ['text', 'value'],
                    data: yearData
                });
                Ext.create('Ext.form.ComboBox', {
                    renderTo: 'wdeComboWrapper',
                    id: 'wdeCombo',
                    labelWidth: 35,
                    emptyText: '请选择',
                    fieldLabel: "年份",
                    valueField: 'value',
                    displayField: 'text',
                    value: firstSelect,
                    editable: false,
                    store: year_store,
                    listeners: {
                        'change': me.changeYear
                    }
                });
                Ext.getCmp('wdeBtn1').fireEvent('click');
                myMask.hide();
            });
    },
    changeYear: function (_this, newVal, oldVal, opt) {
        wdeData.setTableDataByType();
    },
    switchAdminRegion: function () {
        const type = 0;
        wdeData.activeBtn = type;
        wdeData.setTableDataByType();
        _wdeChart.chartType = "0";
        if (_wdeChart.chartid !== null) {
            _wdeChart.change();
        }
    },
    switchWaterRegion: function () {
        const type = 1;
        wdeData.activeBtn = type;
        wdeData.setTableDataByType();
        _wdeChart.chartType = "1";
        if (_wdeChart.chartid !== null) {
            _wdeChart.change();
        }
    },

})
// Ext.define('Admin.view.evaluation.wde.WdeController', {
//     extend: 'Ext.app.ViewController',
//     alias: 'controller.wdeController',
//     views: ['Admin.view.evaluation.wde.Wde'],
//     initLoad: function () {
//         Ext.getCmp('wdeAdminRegion').fireEvent('click');
//     },
//     switchAdminRegion: function () {
//         var me = this;
//         var grid = me.lookupReference('wdeGrid');
//         var store = grid.getStore();
//         var data = [
//             {area: '临川', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '南城', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '黎川', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '南丰', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '崇仁', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '乐安', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '宜黄', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '金溪', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '资溪', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '东乡', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '广昌', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'}
//         ];
//         // grid.getColumns().setColumnHeader(1, '1111');
//         store.loadData(data);
//         var chart_title = '2017年行政分区废污水排放量';
//         setTimeout(function() {
//             me.initChart(chart_title);
//         }, 0);
//     },
//     switchWaterRegion: function () {
//         var me = this;
//         var grid = me.lookupReference('wdeGrid');
//         var store = grid.getStore();
//         var data = [
//             {area: '盱江', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '临水', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '抚河中游廖坊以上', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'},
//             {area: '抚河中游廖坊以下', p1: '25', p2: '1480', p3: '0.37',  p4: '0', p5: '1.2', p6: '23.1', p7: '24.3'}
//         ];
//         // grid.getColumns().setColumnHeader(1, '1111');
//         store.loadData(data);
//         var chart_title = '2017年水资源分区废污水排放量';
//         setTimeout(function() {
//             me.initChart(chart_title);
//         }, 0);
//     },
//     initChart: function (title) {
//         $('#wdeChart').highcharts({
//             chart: {
//                 plotBackgroundColor: null,
//                 plotBorderWidth: null,
//                 plotShadow: false
//             },
//             title: {
//                 text: title
//             },
//             tooltip: {
//                 headerFormat: '{series.name}<br>',
//                 pointFormat: '{point.name}: <b>{point.percentage:.1f}%</b>'
//             },
//             plotOptions: {
//                 pie: {
//                     allowPointSelect: true,
//                     cursor: 'pointer',
//                     dataLabels: {
//                         enabled: true,
//                         format: '<b>{point.name}</b>: {point.percentage:.1f} %',
//                         style: {
//                             color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
//                         }
//                     },
//                     showInLegend: true
//                 }
//             },
//             series: [{
//                 type: 'pie',
//                 size: '80%',
//                 name: '分配比例',
//                 data: [{
//                     y: 4,
//                     name: '城镇居民'
//                 }, {
//                     y: 6,
//                     name: '第二产业'
//                 }, {
//                     y: 2,
//                     name: '第三产业'
//                 }]
//             }],
//             credits: { enabled:false }
//         });
//     }
// })
